package main.java.fiuba;

public class Queue implements IQueue {
	private Node top;
	private Node last;
	private int size;

	public Queue() {
		top = null;
		last = null;
		size = 0;
	}

	/* (non-Javadoc)
	 * @see main.java.IQueue#isEmpty()
	 */
	@Override
	public boolean isEmpty() {
		return (this.size == 0);
	}

	/* (non-Javadoc)
	 * @see main.java.IQueue#size()
	 */
	@Override
	public int size() {
		return this.size;
	}

	/* (non-Javadoc)
	 * @see main.java.IQueue#add(java.lang.Object)
	 */
	@Override
	public void add(Object item){
		Node newNode = new Node(item);
		if (this.isEmpty()){
			this.top = newNode;
			this.last = newNode;
		}
		else{
			this.last.setNext(newNode);
			this.last = this.last.getNext();
		}
		this.addSize();
	}

	/* (non-Javadoc)
	 * @see main.java.IQueue#top()
	 */
	@Override
	public Object top() {
		this.requiresNotEmpty();
		return this.top.getItem();
	}

	/* (non-Javadoc)
	 * @see main.java.IQueue#remove()
	 */
	@Override
	public void remove() {
		this.requiresNotEmpty();
		this.top = this.top.getNext();
		this.decreaseSize();
	}

	private void addSize() {
		this.size++;
	}

	private void decreaseSize() {
		this.size--;
	} 

	private void requiresNotEmpty() throws AssertionError {
		if (this.isEmpty())
			throw new AssertionError("La cola esta vacia");
	} 
}